package main

import (
	"encoding/json"
	"fmt"
	"os"
)

type User struct {
	Id   int
	Name string
	Addr string

	Age int
	Tel string
}

func JsonWfile(fn string, s interface{}) error {
	f, err := os.Create(fn)
	if err != nil {
		return err
	}
	defer f.Close()
	encode := json.NewEncoder(f)
	if err = encode.Encode(&s); err == nil {
		return nil
	}
	return err

}

func main() {
	// user := []User{
	// 	{1, "s1", "xxx", 23, "1111"},
	// 	{2, "s2", "xxx", 23, "1111"},
	// 	{3, "s3", "xxx", 23, "1111"},
	// }
	// f, err := os.Create("a.json")
	// defer f.Close()
	// if err != nil {
	// 	fmt.Println(err)
	// }
	// encode := json.NewEncoder(f)
	// encode.Encode(user)
	// JsonWfile("ab.json", user)

	// if u, err := json.Marshal(user); err == nil {
	// 	fmt.Println(string(u))
	// }

	// ctx := `[{"Id":1,"Name":"s1","Addr":"xxx","Age":23,"Tel":"1111"},{"Id":2,"Name":"s2","Addr":"xxx","Age":23,"Tel":"1111"},{"Id":3,"Name":"s3","Addr":"xxx","Age":23,"Tel":"1111"}]`
	// var user2 []User

	// json.Unmarshal([]byte(ctx), &user2)
	// fmt.Println(user2)
	var user []User
	f, err := os.Open("a.json")
	if err != nil {
		fmt.Println(err)
	}
	defer f.Close()
	decode := json.NewDecoder(f)
	decode.Decode(&user)
	fmt.Println(user)

}
